﻿using B.Auth.APi.Application.Command.Leaves;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Leaves
{
    public class GetBoosNameCommandHand : IRequestHandler<GetBoosNameCommand, ApiResult<List<string>>>
    {
        private readonly IBaseRepository<RoleModel> _role;
        private readonly IBaseRepository<RoleUser> _roleuser;
        private readonly IBaseRepository<User> _user;

        public GetBoosNameCommandHand(IBaseRepository<RoleModel> role, IBaseRepository<RoleUser> roleuser, IBaseRepository<User> user)
        {
            _role = role;
            _roleuser = roleuser;
            _user = user;
        }

        public Task<ApiResult<List<string>>> Handle(GetBoosNameCommand request, CancellationToken cancellationToken)
        {
            //var result= new ApiResult<object>();
            //var roleid = _role.GetAll().FirstOrDefault(x=>x.Boos==1)?.Id;
            //var userid = _roleuser.GetAll().FirstOrDefault(x => x.RoleId == roleid)?.UserId;
            //var BoosName = _user.GetAll().FirstOrDefault(x => x.Id == userid)?.Name;
            //result.data = BoosName;
            //result.Message = "查询boos";
            //return Task.FromResult(result);

            var result = new ApiResult<List<string>>();
            List<int> RoleIds = _role.GetAll().Where(x => x.Boos == 1).Select(x => x.Id).ToList();
            List<int> Userids = new List<int>();
            foreach (var item in RoleIds)
            {

                var userid = _roleuser.GetAll().Where(x => x.RoleId == item).Select(x => x.UserId).ToList();

                Userids.AddRange(userid);
            }
            List<string> userName = new List<string>();
            foreach (var item in Userids)
            {

                var Name = _user.GetAll().Where(x => x.Id == item).Select(x => x.Name).ToList();
                userName.AddRange(Name);

            }

            result.data = userName;
            return Task.FromResult(result);
            

        }
    }
}
